package com.ruoyi.system.service.impl;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.TbVisitorVehicle;
import com.ruoyi.system.domain.TbWeighbridge;
import com.ruoyi.system.domain.dto.DtoCheckUser;
import com.ruoyi.system.domain.dto.DtoVehicle;
import com.ruoyi.system.mapper.TbVisitorVehicleMapper;
import com.ruoyi.system.mapper.TbWeighbridgeMapper;
import com.ruoyi.system.service.ITbVisitorVehicleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 车辆预约信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-04-10
 */
@Service
public class TbVisitorVehicleServiceImpl implements ITbVisitorVehicleService 
{
    @Autowired
    private TbVisitorVehicleMapper tbVisitorVehicleMapper;
    @Resource
    private TbWeighbridgeMapper tbWeighbridgeMapper;

    @Transactional
    @Override
    public AjaxResult saveWeighbridgeInfo(TbWeighbridge tbWeighbridge){
        if (tbWeighbridge.getVehicleId() == null)
        {
            return AjaxResult.error("必要参数错误 ，请确认访客单号。");
        }
        TbVisitorVehicle vehicle = tbVisitorVehicleMapper.selectTbVisitorVehicleById(tbWeighbridge.getVehicleId());
        if (vehicle == null)
        {
            return AjaxResult.error("必要参数错误 ，请确认访客单号。");
        }
        int result = 0;
        //验证新增还是修改
        TbWeighbridge thb = tbWeighbridgeMapper.selectByVehicleId(tbWeighbridge.getVehicleId());
        if (thb != null ){
            tbWeighbridge.setId(thb.getId());
            tbWeighbridge.setCard(vehicle.getCard());
            tbWeighbridge.setGoods(vehicle.getWeighingItems());
            tbWeighbridge.setCreateTime(DateUtils.getNowDate());
            tbWeighbridge.setUpdateTime(DateUtils.getNowDate());
            result = tbWeighbridgeMapper.updateByPrimaryKey(tbWeighbridge);
        }else {
            tbWeighbridge.setCard(vehicle.getCard());
            tbWeighbridge.setGoods(vehicle.getWeighingItems());
            tbWeighbridge.setCreateTime(DateUtils.getNowDate());
            tbWeighbridge.setUpdateTime(DateUtils.getNowDate());
            result = tbWeighbridgeMapper.insert(tbWeighbridge);
        }

        return result > 0 ? AjaxResult.success("操作成功 。") :  AjaxResult.error("操作失败");
    }



    /**
     * 查询车辆预约信息
     * 
     * @param id 车辆预约信息主键
     * @return 车辆预约信息
     */
    @Override
    public TbVisitorVehicle selectTbVisitorVehicleById(Long id)
    {
        return tbVisitorVehicleMapper.selectTbVisitorVehicleById(id);
    }

    @Override
    public TbVisitorVehicle selectTbVisitorVehicleByPlate(String  carPlate)
    {
        return tbVisitorVehicleMapper.selectTbVisitorVehicleByPlate(carPlate);
    }



    /**
     * 查询车辆预约信息(By Open ID)
     *
     * @param openId
     * @return 车辆预约信息
     */
    @Override
    public TbVisitorVehicle selectTbVisitorVehicleByOpenId(String openId)
    {
        return tbVisitorVehicleMapper.selectTbVisitorVehicleByOpenId(openId);
    }

    /**
     * 查询车辆预约信息(By Flow ID)
     *
     * @param flowId
     * @return 车辆预约信息
     */
    @Override
    public TbVisitorVehicle selectTbVisitorVehicleByFlowId(String flowId) {
        return tbVisitorVehicleMapper.selectTbVisitorVehicleByFlowId(flowId);
    }

    /**
     * 查询车辆预约信息列表
     * 
     * @param tbVisitorVehicle 车辆预约信息
     * @return 车辆预约信息
     */
    @Override
    public List<TbVisitorVehicle> selectTbVisitorVehicleList(DtoVehicle tbVisitorVehicle)
    {
        return tbVisitorVehicleMapper.selectTbVisitorVehicleList(tbVisitorVehicle);
    }

    /**
     * 新增车辆预约信息
     * 
     * @param tbVisitorVehicle 车辆预约信息
     * @return 结果
     */
    @Override
    public int insertTbVisitorVehicle(TbVisitorVehicle tbVisitorVehicle)
    {
        tbVisitorVehicle.setCreateTime(DateUtils.getNowDate());
        return tbVisitorVehicleMapper.insertTbVisitorVehicle(tbVisitorVehicle);
    }

    /**
     * 修改车辆预约信息
     * 
     * @param tbVisitorVehicle 车辆预约信息
     * @return 结果
     */
    @Override
    public int updateTbVisitorVehicle(TbVisitorVehicle tbVisitorVehicle)
    {
        tbVisitorVehicle.setUpdateTime(DateUtils.getNowDate());
        return tbVisitorVehicleMapper.updateTbVisitorVehicle(tbVisitorVehicle);
    }

    /**
     * 批量删除车辆预约信息
     * 
     * @param ids 需要删除的车辆预约信息主键
     * @return 结果
     */
    @Override
    public int deleteTbVisitorVehicleByIds(Long[] ids)
    {
        return tbVisitorVehicleMapper.deleteTbVisitorVehicleByIds(ids);
    }

    /**
     * 删除车辆预约信息信息
     * 
     * @param id 车辆预约信息主键
     * @return 结果
     */
    @Override
    public int deleteTbVisitorVehicleById(Long id)
    {
        return tbVisitorVehicleMapper.deleteTbVisitorVehicleById(id);
    }



    /**
     * 查询车辆预约信息信息(列表数据数)
     *
     * @return 网格信息集合
     */
    @Override
    public int selectTbVisitorVehicleCnt(DtoVehicle tbVisitorVehicle)
    {
        List<TbVisitorVehicle> list = tbVisitorVehicleMapper.selectTbVisitorVehicleList(tbVisitorVehicle);
        if (list != null && list.size() > 0) {
            return list.size();
        } else {
            return 0;
        }
    }

    /**
     * 车辆入场查询
     *
     * @param tbVisitorVehicle 车辆预约信息
     * @return 车辆预约信息
     */
    @Override
    public TbVisitorVehicle selectTbVisitorVehicleLimit(DtoVehicle tbVisitorVehicle)
    {
        return tbVisitorVehicleMapper.selectTbVisitorVehicleLimit(tbVisitorVehicle);
    }

    /**
     * 删除车辆预约信息(逻辑删除)
     *
     * @param id 车辆预约信息主键
     * @return 结果
     */
    @Override
    public int deleteLogicTbVisitorVehicleById(Long id)
    {
        return tbVisitorVehicleMapper.deleteLogicTbVisitorVehicleById(id);
    }

    /**
     * 查询重量
     * @param vehicleId
     * @return
     */
    @Override
    public TbWeighbridge selectWeightByVehicleId(Long vehicleId)
    {
        return tbWeighbridgeMapper.selectByVehicleId(vehicleId);
    }

    /**
     * 今日入场货物总量
     * @return
     */
    @Override
    public Long selectAllGrossToday()
    {
        return tbWeighbridgeMapper.selectAllGrossToday();
    }

    /**
     * 查询访客预约信息列表
     *
     * @param tbVisitorUser 访客预约信息
     * @return 访客预约信息集合
     */
    @Override
    public List<TbVisitorVehicle> checkTbVisitorVehicleList(DtoCheckUser tbVisitorUser)
    {
        return tbVisitorVehicleMapper.checkTbVisitorVehicleList(tbVisitorUser);
    }

    private static final Logger log = LoggerFactory.getLogger(TbVisitorVehicleServiceImpl.class);

    /**
     * 文字转语音并播放
     *
     * @param text
     */
    @Override
    public String textToSpeech(String text) {
        ActiveXComponent ax;
        log.debug("----------------Service1---------------");
        // 上传文件路径
        String uploadPath = RuoYiConfig.getUploadPath();
        String fileName = DateUtils.dateTimeNow() + ".mp3";
        String filePath = uploadPath + "/sound/" + fileName;
        log.debug(filePath);
        try {
            ax = new ActiveXComponent("Sapi.SpVoice");
//            // 运行时输出语音内容
            Dispatch spVoice = ax.getObject();
            log.debug("----------------Service1.1---------------");
//            // 音量 0-100
//            ax.setProperty("Volume", new Variant(100));
//            // 语音朗读速度 -10 到 +10
//            ax.setProperty("Rate", new Variant(-2));
//            // 执行朗读
            Dispatch.call(spVoice, "Speak", new Variant(text));

            log.debug("----------------Service2---------------");
            // 下面是构建文件流把生成语音文件
            ax = new ActiveXComponent("Sapi.SpFileStream");
            Dispatch spFileStream = ax.getObject();

            ax = new ActiveXComponent("Sapi.SpAudioFormat");
            Dispatch spAudioFormat = ax.getObject();

            // 设置音频流格式
            Dispatch.put(spAudioFormat, "Type", new Variant(22));
            // 设置文件输出流格式
            Dispatch.putRef(spFileStream, "Format", spAudioFormat);

            log.debug("----------------Service3---------------");
            // 调用输出 文件流打开方法，创建一个.wav文件
            Dispatch.call(spFileStream, "Open", new Variant(filePath), new Variant(3), new Variant(true));
            // 设置声音对象的音频输出流为输出文件对象
            Dispatch.putRef(spVoice, "AudioOutputStream", spFileStream);
//            // 设置音量 0到100
//            Dispatch.put(spVoice, "Volume", new Variant(100));
//            // 设置朗读速度
//            Dispatch.put(spVoice, "Rate", new Variant(-2));
//            // 开始朗读
//            Dispatch.call(spVoice, "Speak", new Variant(text));

            // 关闭输出文件
            Dispatch.call(spFileStream, "Close");
            Dispatch.putRef(spVoice, "AudioOutputStream", null);


            log.debug("----------------Service4---------------");
            spAudioFormat.safeRelease();
            spFileStream.safeRelease();
            spVoice.safeRelease();
            ax.safeRelease();

        } catch (Exception e) {

            log.debug("----------------Service5---------------");
            e.printStackTrace();
            log.debug(e.getMessage());
            log.debug("----------------Service6---------------");
        }

        String url = "http://ps.jhm.com.cn:8890/prod-api/profile/upload/sound/" + fileName;
        return url;
    }
}
